package com.csx.micrometer.common.util;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * @author cuisongxu
 * @date 2022/11/20 20:19
 */
public class MetricsTagUtil {

    public static Tags getTagsFromMap(Map<String, String> map) {
        List<Tag> list = new ArrayList<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            list.add(Tag.of(entry.getKey(), StringUtil.getOrEmpty(entry.getValue())));
        }
        return Tags.of(list);
    }

    public static Set<String> tagValues(Meter.Id id) {
        return StreamSupport.stream(id.getTagsAsIterable().spliterator(), false).map(Tag::getValue).collect(Collectors.toSet());
    }
}
